<template>
  <PageWrapDrawer
:show="show" :isUpdate="props.isUpdate" title-suffix="仓库"
  @success="onSubmit"
  @on-drawer-close="onClose">
    <Form
name="basic"
          :model="insideData.data"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 16 }"
          autocomplete="off"
          layout="horizontal"
    >
      <Row>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="仓库编号" name="code" v-bind="validateInfos['data.code']">
            <a-input v-model:value="insideData.data.code" :maxlength="8">
              <template #addonAfter>
                <Tooltip title="生成编号">
                  <a @click="insideData.data.code = genCode()"><CodeOutlined /></a>
                </Tooltip>
              </template>
            </a-input>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="仓库名称" name="name" v-bind="validateInfos['data.name']">
            <a-input v-model:value="insideData.data.name" :maxlength="8" showCount :allowClear="false"/>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="启用" name="enable">
            <RadioGroup v-model:value="insideData.data.enable">
              <Radio :value="1">启用</Radio>
              <Radio :value="2">禁用</Radio>
            </RadioGroup>
          </FormItem>
        </Col>
        <!-- <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="默认仓库" name="isDefault">
            <RadioGroup v-model:value="insideData.data.isDefault">
              <Radio :value="1">是</Radio>
              <Radio :value="2">不是</Radio>
            </RadioGroup>
          </FormItem>
        </Col> -->
        <!-- <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="单位费用" name="cost">
            <InputNumber v-model:value="insideData.data.cost" :min="0" style="width: 100%"/>
          </FormItem>
        </Col> -->
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="12">
          <FormItem label="仓库电话" name="phone" v-bind="validateInfos['data.phone']">
            <a-input v-model:value="insideData.data.phone"/>
          </FormItem>
        </Col>
        <Col span="24">
          <FormItem
label="地址" name="address"
          :label-col="{ xs: {span: 6}, sm: {span: 6}, md: {span: 6}, lg: {span: 6}, xl: {span: 3}, xxl: {span: 3} }"
          :wrapper-col="{ xs: {span: 16}, sm: {span: 16}, md: {span: 16}, lg: {span: 16}, xl: {span: 20}, xxl: {span: 20} }"
          >
            <a-textarea
v-model:value="insideData.data.address" showCount
            :maxlength="20"
            :autoSize="{minRows: 4}" placeholder="仓库地址"/>
          </FormItem>
        </Col>
        <Col span="24">
          <FormItem
label="备注" name="memo"
          :label-col="{ xs: {span: 6}, sm: {span: 6}, md: {span: 6}, lg: {span: 6}, xl: {span: 3}, xxl: {span: 3} }"
          :wrapper-col="{ xs: {span: 16}, sm: {span: 16}, md: {span: 16}, lg: {span: 16}, xl: {span: 20}, xxl: {span: 20} }"
          >
            <a-textarea
v-model:value="insideData.data.memo" showCount 
            :maxlength="25"
            :autoSize="{minRows: 4}" placeholder="描述一下..."/>
          </FormItem>
        </Col>
      </Row>
    </Form>
  </PageWrapDrawer>
</template>
<script lang="ts" name="userDrawer" setup>
import { CodeOutlined } from '@ant-design/icons-vue';
import {
Col,
Form, FormItem,
Radio, RadioGroup,
Row,
Tooltip
} from 'ant-design-vue';
import { reactive, toRaw, watchPostEffect } from 'vue';
import { create, update } from '/@/api/wms/warehouse';
import { PageWrapDrawer } from '/@/components/PageWrapDrawer';
import { genCode } from "/@/utils/my";

// https://blog.csdn.net/qq_49327308/article/details/124467691
// const {createMessage,} = useMessage();
// const {info, success, warning, error} = createMessage;

const emit = defineEmits(['on-success', 'on-drawer-close'])
const props = defineProps({
    show: Boolean,
    isUpdate: Boolean,
    row: {type: [Object, null] as PropType<Object | null>, default: null,},
})

const useForm = Form.useForm;
const insideData = reactive({
  data: {
    ID: null,
    code: '',
    name: '',
    enable: 1,
    address: '',
    cost: 0,
    isDefault: 0,
    memo: '',
    contacts: '',
    phone: '',
  }
});

const rulesRef = reactive({
  'data.code': [
    {required: true, message: '编码不能为空',},
  ],
  'data.name': [
    {required: true, message: '名称不能为空',},
  ] as unknown,
});
watchPostEffect(async () => {
  let v1 = props.row as any
  if (v1) {
    insideData.data = {...v1}
  }
})

const {resetFields, validate, validateInfos} = useForm(insideData, rulesRef, {
  onValidate: (...args) => {
  },
});

const onSubmit = () => {
  validate()
    .then(async () => {
      let data = toRaw(insideData.data)
      if (props.isUpdate) {
        await update(data).then(() => {
          onClose()
        })
      } else {
        await create(data).then(() => {
          onClose()
        })
      }
      emit('on-success')
    })
}

const onClose = () => {
  Object.assign(insideData, {data: {}});
  resetFields();
  emit('on-drawer-close');
};

</script>
<style scoped>
.a-none {
  pointer-events: none;
}
</style>
